package com.ccys.cms.service.administrator.impl;


import com.ccys.cms.service.administrator.TblAppVersionWebService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.administrator.TblAppVersionDTO;
import com.ccys.db.entity.TblAppVersion;
import com.ccys.db.mapper.TblAppVersionMapper;
import com.ccys.db.page.PageUtils;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.vo.administrator.TblAppVersionWebVO;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * tbl_app_version管理
 */
@Service
@Slf4j
public class TblAppVersionWebServiceImpl extends BaseServiceImpl<TblAppVersion> implements TblAppVersionWebService {

    @Autowired(required = false)
    private TblAppVersionMapper tblAppVersionMapper;

    @Override
    public ResponseResult listObjectsCms(HttpServletRequest request, HttpServletResponse response, TblAppVersionDTO dto) {
        PageUtils.startPage(request);
        List<TblAppVersionWebVO> list = this.tblAppVersionMapper.listObjectsCms(dto);
        PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }


    @Override
    public ResponseResult change(HttpServletRequest request, HttpServletResponse response, TblAppVersion entity) {
        if(entity.getAppType() == null) {
            return ResponseResult.error("AppType is not null");
        }
        List<String> whereList = new ArrayList<>();
        whereList.clear();
        whereList.add("del_flag=0");
        whereList.add("app_type=" + entity.getAppType());
        TblAppVersion oneByLimit = this.findOneByLimit(TblAppVersion.class, whereList);
        if (oneByLimit == null) {
            if(entity.getAppStatus() == null) {
                entity.setAppStatus(1);
            }
            // 新增
            this.saveEntity(entity);
        } else {
            // 编辑
            entity.setId(oneByLimit.getId());
            this.updateEntityById(entity);
        }
        return ResponseResult.success();
    }




}